
PREFIX = $(DESTDIR)@prefix@

CC = @CC@
CXX = @CXX@
AR = @AR@

CFLAGS = @CFLAGS@
CXXFLAGS = @CXXFLAGS@
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@

# unix or windows
PLATFORM = @PLATFORM@

# 1 to compile C++ wrappers
ENABLE_CXX = @ENABLE_CXX@

# 1 to enable SPDY support
ENABLE_SPDY = @ENABLE_SPDY@

# 1 to enable SSL support
ENABLE_SSL = @ENABLE_SSL@

SO_EXT = @SO_EXT@
SO_NAME = @SO_NAME@
SONAME_FLAGS = @SONAME_FLAGS@
REAL_NAME = liblacewing.@SO_EXT@.@PACKAGE_VERSION@

OBJECTS =\
build/global.o \
build/nvhash.o \
build/filter.o \
build/address.o \
build/streamgraph.o \
build/stream.o \
build/error.o \
build/webserver.o \
build/http-client.o \
build/http-parse.o \
build/http-parser.o \
build/mimetypes.o \
build/request.o \
build/sessions.o \
build/pipe.o \
build/multipart.o \
build/flashpolicy.o \
build/pump.o \
build/util.o \
build/list.o \
build/heapbuffer.o \
build/upload.o \
build/multipart_parser.o

COMMONDEPS =\
include/lacewing.h \
src/common.h \
src/heapbuffer.h \
src/list.h \
src/refcount.h \
src/webserver/common.h \
src/webserver/http/http.h \
src/address.h \
src/stream.h

ifeq ($(ENABLE_CXX), 1)
OBJECTS +=\
build/address-cxx.o \
build/client-cxx.o \
build/error-cxx.o \
build/event-cxx.o \
build/eventpump-cxx.o \
build/fdstream-cxx.o \
build/file-cxx.o \
build/filter-cxx.o \
build/flashpolicy-cxx.o \
build/pipe-cxx.o \
build/pump-cxx.o \
build/server-cxx.o \
build/stream-cxx.o \
build/sync-cxx.o \
build/thread-cxx.o \
build/timer-cxx.o \
build/udp-cxx.o \
build/webserver-cxx.o
endif

ifeq ($(PLATFORM), unix)
OBJECTS +=\
build/unix-client.o \
build/unix-event.o \
build/unix-eventpump.o \
build/unix-server.o \
build/unix-thread.o \
build/unix-timer.o \
build/unix-udp.o \
build/unix-fdstream.o \
build/unix-file.o \
build/unix-sync.o \
build/unix-global.o
ifeq ($(ENABLE_SSL), 1)
OBJECTS += build/openssl-sslclient.o
endif
COMMONDEPS +=\
src/unix/eventpump.h \
src/unix/common.h
endif


ifeq ($(PLATFORM), windows)
OBJECTS +=\
build/windows-client.o \
build/windows-compat.o \
build/windows-event.o \
build/windows-eventpump.o \
build/windows-server.o \
build/windows-thread.o \
build/windows-timer.o \
build/windows-udp.o \
build/windows-fdstream.o \
build/windows-file.o \
build/windows-sslclient.o \
build/windows-sync.o \
build/windows-global.o
COMMONDEPS +=\
src/windows/compat.h \
src/windows/common.h
endif

ifeq ($(ENABLE_SPDY), 1)
OBJECTS +=\
build/spdy_bytes.o \
build/spdy_control.o \
build/spdy_credential.o \
build/spdy_ctx.o \
build/spdy_goaway.o \
build/spdy_headers.o \
build/spdy_nv_block.o \
build/spdy_ping.o \
build/spdy_rst_stream.o \
build/spdy_settings.o \
build/spdy_stream.o \
build/spdy_strings.o \
build/spdy_syn_reply.o \
build/spdy_syn_stream.o \
build/spdy_zlib.o \
build/spdy_window_update.o \
build/spdy.o \
build/spdy-client.o
endif

###########

all: liblacewing.@SO_EXT@ liblacewing.a

liblacewing.@SO_EXT@: $(OBJECTS)
	@echo Linking shared library...
ifeq ($(ENABLE_CXX), 1)
	@$(CXX) $(CXXFLAGS) $(SONAME_FLAGS) -shared -o $(REAL_NAME) $(OBJECTS) $(LIBS)
else
	@$(CC) $(CFLAGS) $(SONAME_FLAGS) -shared -o $(REAL_NAME) $(OBJECTS) $(LIBS)
endif

liblacewing.a: $(OBJECTS)
	@echo Linking static library...
	@rm -f liblacewing.a
	@$(AR) rcs liblacewing.a ./build/*.o

build/global.o: src/global.c $(COMMONDEPS) 
	$(CC) $(CFLAGS) -c -o $@ src/global.c
build/nvhash.o: src/nvhash.c $(COMMONDEPS) 
	$(CC) $(CFLAGS) -c -o $@ src/nvhash.c
build/util.o: src/util.c $(COMMONDEPS) 
	$(CC) $(CFLAGS) -c -o $@ src/util.c
build/heapbuffer.o: src/heapbuffer.c $(COMMONDEPS) 
	$(CC) $(CFLAGS) -c -o $@ src/heapbuffer.c
build/list.o: src/list.c $(COMMONDEPS) 
	$(CC) $(CFLAGS) -c -o $@ src/list.c
build/filter.o: src/filter.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/filter.c
build/address.o: src/address.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/address.c
build/stream.o: src/stream.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/stream.c
build/streamgraph.o: src/streamgraph.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/streamgraph.c
build/pipe.o: src/pipe.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/pipe.c
build/error.o: src/error.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/error.c
build/webserver.o: src/webserver/webserver.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/webserver.c
build/request.o: src/webserver/request.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/request.c
build/upload.o: src/webserver/upload.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/upload.c
build/sessions.o: src/webserver/sessions.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/sessions.c
build/mimetypes.o: src/webserver/mimetypes.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/mimetypes.c
build/multipart.o: src/webserver/multipart.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/multipart.c
build/http-client.o: src/webserver/http/http-client.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/http/http-client.c
build/http-parse.o: src/webserver/http/http-parse.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/http/http-parse.c
build/spdy.o: src/webserver/spdy/spdy.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/spdy/spdy.c
build/spdy-client.o: src/webserver/spdy/spdy-client.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/webserver/spdy/spdy-client.c
build/http-parser.o: deps/http-parser/http_parser.c deps/http-parser/http_parser.h
	$(CC) $(CFLAGS) -DHTTP_PARSER_STRICT=0 -DHTTP_PARSER_DEBUG=0 -c -o $@ deps/http-parser/http_parser.c
build/multipart_parser.o: deps/multipart-parser/multipart_parser.c deps/multipart-parser/multipart_parser.h
	$(CC) $(CFLAGS) -c -o $@ deps/multipart-parser/multipart_parser.c
build/flashpolicy.o: src/flashpolicy.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/flashpolicy.c
build/pump.o: src/pump.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/pump.c

build/openssl-sslclient.o: src/openssl/sslclient.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/openssl/sslclient.c

build/unix-event.o: src/unix/event.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/event.c
build/unix-eventpump.o: src/unix/eventpump.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/eventpump.c
build/unix-server.o: src/unix/server.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/server.c
build/unix-timer.o: src/unix/timer.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/timer.c
build/unix-udp.o: src/unix/udp.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/udp.c
build/unix-thread.o: src/unix/thread.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/thread.c
build/unix-sync.o: src/unix/sync.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/sync.c
build/unix-client.o: src/unix/client.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/client.c
build/unix-global.o: src/unix/global.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/global.c
build/unix-fdstream.o: src/unix/fdstream.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/fdstream.c
build/unix-file.o: src/unix/file.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/unix/file.c

build/windows-event.o: src/windows/event.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/event.c
build/windows-eventpump.o: src/windows/eventpump.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/eventpump.c
build/windows-server.o: src/windows/server.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/server.c
build/windows-timer.o: src/windows/timer.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/timer.c
build/windows-udp.o: src/windows/udp.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/udp.c
build/windows-thread.o: src/windows/thread.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/thread.c
build/windows-client.o: src/windows/client.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/client.c
build/windows-compat.o: src/windows/compat.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/compat.c
build/windows-fdstream.o: src/windows/fdstream.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/fdstream.c
build/windows-file.o: src/windows/file.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/file.c
build/windows-sync.o: src/windows/sync.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/sync.c
build/windows-sslclient.o: src/windows/sslclient.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/sslclient.c
build/windows-global.o: src/windows/global.c $(COMMONDEPS)
	$(CC) $(CFLAGS) -c -o $@ src/windows/global.c

build/spdy_bytes.o: deps/spdy/src/spdy_bytes.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_bytes.c
build/spdy_control.o: deps/spdy/src/spdy_control.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_control.c
build/spdy_credential.o: deps/spdy/src/spdy_credential.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_credential.c
build/spdy_ctx.o: deps/spdy/src/spdy_ctx.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_ctx.c
build/spdy_goaway.o: deps/spdy/src/spdy_goaway.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_goaway.c
build/spdy_headers.o: deps/spdy/src/spdy_headers.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_headers.c
build/spdy_window_update.o: deps/spdy/src/spdy_window_update.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_window_update.c
build/spdy_nv_block.o: deps/spdy/src/spdy_nv_block.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_nv_block.c
build/spdy_ping.o: deps/spdy/src/spdy_ping.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_ping.c
build/spdy_rst_stream.o: deps/spdy/src/spdy_rst_stream.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_rst_stream.c
build/spdy_settings.o: deps/spdy/src/spdy_settings.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_settings.c
build/spdy_stream.o: deps/spdy/src/spdy_stream.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_stream.c
build/spdy_syn_reply.o: deps/spdy/src/spdy_syn_reply.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_syn_reply.c
build/spdy_syn_stream.o: deps/spdy/src/spdy_syn_stream.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_syn_stream.c
build/spdy_zlib.o: deps/spdy/src/spdy_zlib.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_zlib.c
build/spdy_strings.o: deps/spdy/src/spdy_strings.c
	$(CC) $(CFLAGS) -c -o $@ deps/spdy/src/spdy_strings.c

build/address-cxx.o: src/cxx/address.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/address.cc
build/client-cxx.o: src/cxx/client.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/client.cc
build/error-cxx.o: src/cxx/error.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/error.cc
build/event-cxx.o: src/cxx/event.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/event.cc
build/eventpump-cxx.o: src/cxx/eventpump.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/eventpump.cc
build/fdstream-cxx.o: src/cxx/fdstream.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/fdstream.cc
build/file-cxx.o: src/cxx/file.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/file.cc
build/filter-cxx.o: src/cxx/filter.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/filter.cc
build/flashpolicy-cxx.o: src/cxx/flashpolicy.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/flashpolicy.cc
build/pipe-cxx.o: src/cxx/pipe.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/pipe.cc
build/pump-cxx.o: src/cxx/pump.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/pump.cc
build/server-cxx.o: src/cxx/server.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/server.cc
build/stream-cxx.o: src/cxx/stream.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/stream.cc
build/sync-cxx.o: src/cxx/sync.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/sync.cc
build/thread-cxx.o: src/cxx/thread.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/thread.cc
build/timer-cxx.o: src/cxx/timer.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/timer.cc
build/udp-cxx.o: src/cxx/udp.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/udp.cc
build/webserver-cxx.o: src/cxx/webserver.cc $(COMMONDEPS)
	$(CXX) $(CXXFLAGS) -c -o $@ src/cxx/webserver.cc

############
	
clean:
	rm -f liblacewing.@SO_EXT@* liblacewing.a build/*.o

install: liblacewing.@SO_EXT@ liblacewing.a
	@echo -----
	@echo Installing shared library: $(PREFIX)/lib/liblacewing.@SO_EXT@
	@install -d $(PREFIX)/lib
	@install -m 0755 $(REAL_NAME) $(PREFIX)/lib/$(REAL_NAME)
	@rm -f $(PREFIX)/lib/$(SO_NAME)
	@ln -s $(REAL_NAME) $(PREFIX)/lib/$(SO_NAME)
	@rm -f $(PREFIX)/lib/liblacewing.@SO_EXT@
	@ln -s $(SO_NAME) $(PREFIX)/lib/liblacewing.@SO_EXT@
	@echo Installing static library: $(PREFIX)/lib/liblacewing.a
	@install -m 0755 liblacewing.a $(PREFIX)/lib/liblacewing.a
	@echo Installing header file: $(PREFIX)/include/lacewing.h
	@install -d $(PREFIX)/include
	@install -m 0644 ./include/lacewing.h $(PREFIX)/include/lacewing.h
	@echo -----
	@echo Compiler flags: -I$(PREFIX)/include
	@echo Linker flags: -L$(PREFIX)/lib -llacewing
	@echo ------

############

test: 
	cc $(CFLAGS) -o list test/list.c
	./list

EXAMPLE_FLAGS = -Iinclude liblacewing.a $(LIBS) 

examples: liblacewing.a
	$(CC) examples/hello_world/hello_world.c -o examples/hello_world/hello_world $(EXAMPLE_FLAGS)
	$(CC) examples/send_file/send_file.c -o examples/send_file/send_file $(EXAMPLE_FLAGS)

examples-cxx: liblacewing.a
	$(CXX) examples/ajax/ajax.cc -o examples/ajax/ajax_cxx $(EXAMPLE_FLAGS)
	$(CXX) examples/get/get.cc -o examples/get/get_cxx $(EXAMPLE_FLAGS)
	$(CXX) examples/hello_world/hello_world.cc -o examples/hello_world/hello_world_cxx $(EXAMPLE_FLAGS)
	$(CXX) examples/post/post.cc -o examples/post/post_cxx $(EXAMPLE_FLAGS)
	$(CXX) examples/send_file/send_file.cc -o examples/send_file/send_file_cxx $(EXAMPLE_FLAGS)
	$(CXX) examples/sessions/sessions.cc -o examples/sessions/sessions_cxx $(EXAMPLE_FLAGS)
	$(CXX) examples/upload_file/upload_file.cc -o examples/upload_file/upload_file_cxx $(EXAMPLE_FLAGS)

.PHONY: all clean install test examples-cxx examples

